<?php
require_once("PHPExcel/PHPExcel.php");
require_once("PHPExcel/Reader/Excel2007.php");
require_once("PHPExcel/Reader/Excel5.php");
// require_once ('PHPExcel/IOFactory.php');
class Product_Service_Upload
{
	/**
	* 读取CSV文件
	* @param string $filePath
	* @return array
	*/
	public static function readCSV($filePath){
		$content = file_get_contents($filePath);
		$arr = preg_split('/\n/',$content);	
		$data = array();
		foreach($arr as $k=>$v){
			if($v){			
				$data[] = explode(",",trim($v));
			}
		}
		return $data;
	}	
	
	/**
	* 读取EXCEL文件
	* @param string $filePath
	* @return array
	*/
	public static function readEXCEL($filePath){
		$PHPExcel = new PHPExcel();
		$PHPReader = new PHPExcel_Reader_Excel2007();
		if(!$PHPReader->canRead($filePath)){
			$PHPReader = new PHPExcel_Reader_Excel5();
			if(!$PHPReader->canRead($filePath)){
				return "Invalid File.";
			}
		}
		$PHPExcel = $PHPReader->load($filePath);
		$currentSheet = $PHPExcel->getSheet(0);

		/**取得一共有多少列*/
		$maxColumn = $currentSheet->getHighestColumn();
		/**取得一共有多少行*/
		$rowCount = $currentSheet->getHighestRow();
		$result = array();
		for($row = 1;$row <= $rowCount;$row++){
			$totalLen = 0;//记录行总长度
			for($column="A";$column<=$maxColumn;$column++){
				$value = $currentSheet->getCell($column.$row)->getValue();
				if(is_object($value)){
					$value =  $value->__toString();
				}
				$result[$row][] = $value;
				$totalLen += strlen(trim($value));
			}
			if($totalLen == 0) unset($result[$row]);//去掉空行
		}
//		print_r(array_values($result));die();
		return  array_values($result);
	}
}